﻿using CommonServiceLocator;
using HuiZe.education.ViewModel;
using System.Windows.Controls;
using System.Windows;
using System;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Forms;

namespace HuiZe.education.View
{
    /// <summary>
    /// AccountLoginView.xaml 的交互逻辑
    /// </summary>
    public partial class AccountLoginView : System.Windows.Controls.UserControl
    {
        System.Windows.Forms.WebBrowser webBrowser;
        System.Windows.Forms.WebBrowser webBrowser2;
        System.Windows.Forms.WebBrowser webBrowser3;

        LoginViewModel loginViewModel1 { get; set; }
        public AccountLoginView()
        {
            InitializeComponent();
            Loaded += AccountLoginView_Loaded;
        }

        private void AccountLoginView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            LoginViewModel loginViewModel = DataContext as LoginViewModel;
            loginViewModel.AccountView = this;
            account.SelectionStart = account.Text.Length;
            loginViewModel1 = loginViewModel;
            webBrowser = new System.Windows.Forms.WebBrowser();
            webBrowser.ScriptErrorsSuppressed = true;


            webBrowser2 = new System.Windows.Forms.WebBrowser();
            webBrowser2.ScriptErrorsSuppressed = true;

            //webBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
            //webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
            webBrowser.DocumentCompleted += webBrowser1_DocumentCompleted;
            webBrowser2.DocumentCompleted += webBrowser2_DocumentCompleted;

        }

        private void pwd_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {

            LoginViewModel loginViewModel = ServiceLocator.Current.GetInstance<LoginViewModel>();
            loginViewModel.ErrorMsg.LoginPwdError = "";
        }

        private void YCWateTextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            LoginViewModel loginViewModel = ServiceLocator.Current.GetInstance<LoginViewModel>();
            loginViewModel.ErrorMsg.LoginMobileError = "";
        }

        private void CustomButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //bg_addStu.Visibility = true;
            LoginViewModel loginViewModel = ServiceLocator.Current.GetInstance<LoginViewModel>();
            loginViewModel.WXLogin();
            //QrcodeView2 myInsert = new QrcodeView2();
            //myInsert.ShowDialog();
        }

        private void CustomButton_Click_1(object sender, RoutedEventArgs e)
        {
            LoginViewModel loginViewModel = ServiceLocator.Current.GetInstance<LoginViewModel>();
            loginViewModel.QQLogin();
        }


        public static Image get_Fill_image(string url)
        {
            var image = new Image();
            image.Source = new BitmapImage(new Uri(url, UriKind.Absolute));
            image.Stretch = Stretch.Fill;
            return image;
        }




        public void ShowImg(string url)
        {
            //webBrowser.ScriptErrorsSuppressed = true;
            // webBrowser.Navigate(url);
            ShowQQLogin showQQLogin = new ShowQQLogin();
            showQQLogin.LoadType = 2;
            showQQLogin.DataContext = loginViewModel1;
            showQQLogin.loadUrl = url;
            showQQLogin.ShowDialog();

        }

        public void ShowQQImg(string url)
        {

            //webBrowser.Navigate(url);
            ShowQQLogin showQQLogin = new ShowQQLogin();
            showQQLogin.LoadType = 1;
            showQQLogin.loadUrl = url;
            showQQLogin.DataContext = loginViewModel1;
            showQQLogin.ShowDialog();








        }
        public void ShowQQImg2(string url)
        {

            //webBrowser.Navigate(url);
            ShowQQLogin showQQLogin = new ShowQQLogin();
            showQQLogin.LoadType = 4;
            showQQLogin.loadUrl = url;
            showQQLogin.DataContext = loginViewModel1;
            showQQLogin.ShowDialog();








        }

        public string NavigateUrl { get; set; }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            try
            {

                if (webBrowser.Url.Query.ToString().Contains("code="))
                {
                    string query = webBrowser.Url.Query.ToString();
                    string code = query.Substring(query.IndexOf("code=") + 5, query.IndexOf('&') - (query.IndexOf("code=") + 5));
                    msgDetail.Visibility = Visibility.Hidden;
                    LoginViewModel loginViewModel = DataContext as LoginViewModel;
                    loginViewModel.OtherLogin(code, "Wechat");

                }
                var a = webBrowser.Document.Body.Children;



                foreach (HtmlElement he in webBrowser.Document.All)
                {
                    string str = he.GetAttribute("InnerText");
                    if (str.Contains("QQ帐号安全登录"))
                    {

                        foreach (HtmlElement he2 in he.All)
                        {
                            string qrcodeUrl = he2.GetAttribute("src");
                            if (!string.IsNullOrWhiteSpace(qrcodeUrl))
                            {

                                webBrowser2.Navigate(qrcodeUrl);
                                //NavigateUrl = qrcodeUrl;
                                //FormsWebBrowser formswebBrowser = new FormsWebBrowser(this.webBrowserDockPanel);
                                //formswebBrowser.WebBrowser.Navigate(NavigateUrl);
                                //msgDetail2.Visibility = Visibility.Visible;
                            }

                        }
                    }
                }


                foreach (HtmlElement he in webBrowser.Document.GetElementsByTagName("img"))
                {
                    string str = he.GetAttribute("className");
                    if (str == "qrcode lightBorder")
                    {
                        string qrcodeUrl = he.GetAttribute("src");
                        Image image = get_Fill_image(qrcodeUrl);
                        QrImg.Source = image.Source;
                        msgDetail.Visibility = Visibility.Visible;
                    }
                }

            }
            catch { }



        }


        private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            try
            {
                // ((System.Windows.Forms.WebBrowser)sender).Document.Window.Error +=  new HtmlElementErrorEventHandler(Window_Error);


                foreach (HtmlElement he in webBrowser.Document.All)
                {
                    bool isbreak = false;


                    foreach (HtmlElement he2 in he.All)
                    {
                        string qrcodeUrl = he2.GetAttribute("src");

                        if (qrcodeUrl.Contains("xui.ptlogin2.qq.com"))
                        {
                            //ShowQQLogin showQQLogin = new ShowQQLogin();
                            //showQQLogin.loadUrl = qrcodeUrl;
                            //showQQLogin.ShowDialog();
                            //FormsWebBrowser formswebBrowser = new FormsWebBrowser(this.webBrowserDockPanel);
                            //formswebBrowser.WebBrowser.Navigate(qrcodeUrl);
                            //msgDetail2.Visibility = Visibility.Visible;
                            isbreak = true;
                            break;
                        }


                    }
                    if (isbreak)
                        break;


                }

                //if (webBrowser.Document.Body.Document.Url.ToString().Contains("login_text=授权并登录"))
                //{
                //    string url = webBrowser.Document.Body.Document.Url.ToString();




                //}



            }
            catch { }



        }

        private void exitAddStu_Click(object sender, RoutedEventArgs e)
        {
            //msgDetail.Visibility = Visibility.Hidden;
            msgDetail.Visibility = Visibility.Collapsed;
        }
    }
}
